import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:my_app/app/modules/dio_tool/dio_tool_controller.dart';
import 'package:my_app/app/core/theme/app_text_theme.dart';
import 'package:my_app/app/widgets/custom_title_widget.dart';

/// dio_tool_page.dart
///
/// Created by fuchuanwei on Tue Feb 21 2023.
///
/// Description: Dio工具 页面
///
class DioToolPage extends GetView<DioToolController> {
  @override
  Widget build(BuildContext context) {
    Get.put(DioToolController());
    return Scaffold(
      appBar: AppBar(title: Text('Dio网络请求')),
      body: ListView(
        padding: EdgeInsets.all(10.0),
        children: [
          requestDemo(),
          SizedBox(height: 20.0),
          paramsConfig(),
          SizedBox(height: 20.0),
          responseModelDemo(),
        ],
      ),
    );
  }

  ///
  ///基础请求示例
  ///
  Widget requestDemo() {
    return Card(
      child: Padding(
        padding: EdgeInsets.all(10.0),
        child: Column(
          children: [
            Text(
              '基础请求示例',
              style: cardTitle,
            ),
            Text(
              '请求结果为:',
              style: tips,
            ),
            Container(
              padding: EdgeInsets.all(10.0),
              decoration: BoxDecoration(
                  color: Colors.grey[300],
                  borderRadius: BorderRadius.circular(10.0)),
              child: Obx(() => Text(controller.responseStr.value)),
            )
          ],
        ),
      ),
    );
  }

  ///参数配置
  Widget paramsConfig() {
    return Column(
      children: [
        Text(
          '参数配置',
          style: cardTitle,
        ),
        SizedBox(height: 10.0),
        Column(
          children: [
            CustomTitleWidget(
              title: '请求方式',
            ),
            SizedBox(height: 5.0),
            Obx(() => SizedBox(
                  width: double.infinity,
                  child: CupertinoSegmentedControl(
                    padding: EdgeInsets.zero,
                    groupValue: controller.selectedTypeOption.value,
                    children: {
                      0: Text('GET'),
                      1: Text('POST'),
                      2: Text('PUT'),
                      3: Text('DELETE')
                    },
                    onValueChanged: (int value) =>
                        controller.changeSelectedTypeOption(value),
                  ),
                ))
          ],
        ),
        SizedBox(height: 20.0),
      ],
    );
  }

  ///请求返回实体演演示
  Widget responseModelDemo() {
    return Card(
      child: Padding(
        padding: EdgeInsets.all(10.0),
        child: Column(
          children: [
            Text(
              '请求响应实体',
              style: cardTitle,
            ),
            Text(
              '请求结果为:',
              style: tips,
            ),
            Container(
              padding: EdgeInsets.all(10.0),
              decoration: BoxDecoration(
                  color: Colors.grey[300],
                  borderRadius: BorderRadius.circular(10.0)),
              child:
                  Obx(() => Text(jsonEncode(controller.user.value.toJson()))),
            )
          ],
        ),
      ),
    );
  }
}
